﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.Composition;
using System.Web.Security;

namespace UnfriendlyFire.Controllers
{
    [Export(typeof(IAccountServices))]
    public class FormsAccountServices : IAccountServices
    {
        public void SignUp(string email, string password, string passwordQuestion, string passwordAnswer, bool staySignedIn)
        {
            try
            {
                MembershipCreateStatus status;
                Membership.CreateUser(email, password, email, passwordQuestion, passwordAnswer, true, out status);
                if (status == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(email, staySignedIn);
                }
                else
                {
                    ThrowSignUpException(status);
                }
            }
            catch (MembershipCreateUserException e)
            {
                ThrowSignUpException(e.StatusCode);                
            }
        }

        private static void ThrowSignUpException(MembershipCreateStatus status)
        {
            switch (status)
            {
                case MembershipCreateStatus.DuplicateEmail:
                    throw new SignUpException("Email", "Duplicate Email");
                case MembershipCreateStatus.DuplicateUserName:
                    throw new SignUpException("Email", "Duplicate Email");
                case MembershipCreateStatus.InvalidEmail:
                    throw new SignUpException("Email", "Invalid Email");
                case MembershipCreateStatus.InvalidPassword:
                    throw new SignUpException("Password", "Invalid Password");
                case MembershipCreateStatus.InvalidUserName:
                    throw new SignUpException("Email", "Invalid Email");
                case MembershipCreateStatus.DuplicateProviderUserKey:
                case MembershipCreateStatus.InvalidAnswer:
                case MembershipCreateStatus.InvalidProviderUserKey:
                case MembershipCreateStatus.InvalidQuestion:
                case MembershipCreateStatus.ProviderError:
                case MembershipCreateStatus.Success:
                case MembershipCreateStatus.UserRejected:
                default:
                    throw new SignUpException(string.Empty, "Unknown Error");
            }
        }

        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }

        public void SignIn(string email, string password, bool staySignedIn)
        {
            if (Membership.ValidateUser(email, password))
            {
                FormsAuthentication.SetAuthCookie(email, staySignedIn);
            }
            else
            {
                throw new SignInException(string.Empty, "User name and password not recognized");
            }
        }
    }
}